import {
  Body,
  Controller,
  Del,
  Get,
  Inject,
  Post,
  Put,
  Query,
} from '@midwayjs/core';
import { ArticleService } from '../../service/article/article.service';
import { Validate } from '@midwayjs/validate';
import { ListParamsValidator, StrIdValidator } from '../../validator';
import {
  EditArticleValidator,
  SaveArticleValidator,
} from '../../validator/article/article';
import { ArticleSchema } from '../../schema';

@Controller('/article', { tagName: '' })
export class ArticleController {
  @Inject()
  articleService: ArticleService;

  @Post('/list', { summary: '列表' })
  @Validate()
  async list(@Body() body: ListParamsValidator<ArticleSchema>) {
    const data = await this.articleService.list(body);
    return Object.assign(data, body);
  }

  @Post('/save', { summary: '保存' })
  @Validate()
  async save(@Body() body: SaveArticleValidator) {
    return await this.articleService.save(body);
  }

  @Get('/info', { summary: '信息' })
  @Validate()
  async info(@Query() body: StrIdValidator) {
    return await this.articleService.info(body.id);
  }

  @Del('/remove', { summary: '删除' })
  @Validate()
  async remove(@Query() body: StrIdValidator) {
    return await this.articleService.remove(body.id);
  }

  @Del('/delete', { summary: '（真）删除' })
  @Validate()
  async realRemove(@Query() body: StrIdValidator) {
    return await this.articleService.realRemove(body.id);
  }

  @Put('/edit', { summary: '编辑' })
  @Validate()
  async edit(
    @Query() query: StrIdValidator,
    @Body() body: EditArticleValidator
  ) {
    const article = await this.articleService.info(query.id);
    Object.assign(article, body);
    return await this.articleService.save(article);
  }

  @Put('/onOff', { summary: '开启关闭文章' })
  @Validate()
  async onOff(@Query() query: StrIdValidator) {
    const article = await this.articleService.info(query.id);
    Object.assign(article, { off: !article.off });
    return await this.articleService.save(article);
  }
}
